<?php


class PhotoTable extends Doctrine_Table
{
  public static function getInstance()
  {
      return Doctrine_Core::getTable('Photo');
  }
  
  public function getBackendListQuery(Doctrine_Query $query)
  {
    $rootAlias = $query->getRootAlias();
    
    $user = sfContext::getInstance()->getUser();
    
    if ($user->hasCredential('editor'))
    {
      $query->andWhere('publisher_id = ?', $user->getAttribute('id'));
    }
    else if ($user->hasCredential('admin'))
    {
      $query->andWhereIn('area_id', $user->getAreaIds());
    }

    return $query->innerJoin("$rootAlias.publisher p")
      ->innerJoin("$rootAlias.area a");
  }
  
  public function getWithCategoryQuery()
  {
    return $this->createQuery('p')
      ->innerJoin('p.categories c')
      ->orderBy('p.id desc')
      ->limit(10);
  }
  
  public function getByCategoryQuery($category)
  {
    return $this->getWithCategoryQuery()
      ->addWhere('c.category = ?', $category);
  }
}